﻿using System;
using System.Globalization;
using System.IO;
using System.Text;

using MbUnit.Framework;

namespace Abstract2ion.Tests
{
	public class ConsoleWrapperWriteTestFixture
	{
		private StringBuilder _consoleOutput;
		private ConsoleWrapper _consoleWrapper;

		[SetUp]
		public void Setup()
		{
			_consoleOutput = new StringBuilder();
			Console.SetOut(new StringWriter(_consoleOutput, CultureInfo.CurrentCulture));
			_consoleWrapper = new ConsoleWrapper();
		}

		[TearDown]
		public void Teardown()
		{
			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = true});
			Console.ResetColor();
		}

		[Test]
		public void ShouldWriteBooleanToConsoleOutput()
		{
			_consoleWrapper.Write(true);

			Assert.AreEqual("True", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineBooleanToConsoleOutput()
		{
			_consoleWrapper.WriteLine(true);

			Assert.AreEqual("True" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteCharToConsoleOutput()
		{
			_consoleWrapper.Write('b');

			Assert.AreEqual("b", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineCharToConsoleOutput()
		{
			_consoleWrapper.WriteLine('c');

			Assert.AreEqual("c" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteCharArrayToConsoleOutput()
		{
			_consoleWrapper.Write(new [] {'b', 'e' });

			Assert.AreEqual("be", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineCharArrayToConsoleOutput()
		{
			_consoleWrapper.WriteLine(new[] { 'q', 'w' });

			Assert.AreEqual("qw" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteDecimalToConsoleOutput()
		{
			_consoleWrapper.Write(0.67m);

			Assert.AreEqual("0.67", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineDecimalToConsoleOutput()
		{
			_consoleWrapper.WriteLine(5.6m);

			Assert.AreEqual("5.6" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteDoubleToConsoleOutput()
		{
			_consoleWrapper.Write(1.23);

			Assert.AreEqual("1.23", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineDoubleToConsoleOutput()
		{
			_consoleWrapper.WriteLine(9.87);

			Assert.AreEqual("9.87" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteInt32ToConsoleOutput()
		{
			_consoleWrapper.Write(123);

			Assert.AreEqual("123", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineInt32ToConsoleOutput()
		{
			_consoleWrapper.WriteLine(333);

			Assert.AreEqual("333" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteInt64ToConsoleOutput()
		{
			_consoleWrapper.Write(155L);

			Assert.AreEqual("155", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineInt64ToConsoleOutput()
		{
			_consoleWrapper.WriteLine(345L);

			Assert.AreEqual("345" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteObjectToConsoleOutput()
		{
			_consoleWrapper.Write(new WriteableObject("Writeable"));

			Assert.AreEqual("Writeable", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineObjectToConsoleOutput()
		{
			_consoleWrapper.WriteLine(new WriteableObject("Writeable"));

			Assert.AreEqual("Writeable" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteFloatToConsoleOutput()
		{
			_consoleWrapper.Write(12.3f);

			Assert.AreEqual("12.3", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineFloatToConsoleOutput()
		{
			_consoleWrapper.WriteLine(33.3f);

			Assert.AreEqual("33.3" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteStringToConsoleOut()
		{
			_consoleWrapper.Write("Test String");

			Assert.AreEqual("Test String", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineStringToConsoleOut()
		{
			_consoleWrapper.WriteLine("Test String");

			Assert.AreEqual("Test String" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteFormattedObjectToConsoleOut()
		{
			_consoleWrapper.Write("{0:X}", 123);

			Assert.AreEqual("7B", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineFormattedObjectToConsoleOut()
		{
			_consoleWrapper.WriteLine("{0:X}", 123);

			Assert.AreEqual("7B" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteFormattedStringToConsoleOut()
		{
			_consoleWrapper.Write("{0:X}{1}", 123, 456);

			Assert.AreEqual("7B456", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineFormattedStringToConsoleOut()
		{
			_consoleWrapper.WriteLine("{0:X}{1}", 123, 789);

			Assert.AreEqual("7B789" + Environment.NewLine, _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteArraySectionToConsoleOut()
		{
			_consoleWrapper.Write("blah".ToCharArray(), 1, 2);

			Assert.AreEqual("la", _consoleOutput.ToString());
		}

		[Test]
		public void ShouldWriteLineArraySectionToConsoleOut()
		{
			_consoleWrapper.WriteLine("blah".ToCharArray(), 1, 2);

			Assert.AreEqual("la" + Environment.NewLine, _consoleOutput.ToString());
		}
	}
}